<?php

namespace app\member\controller;

/** 控制器类引入区 **/
use app\common\controller\Smscm;

/** 模型类引入区 **/
use app\common\model\Project as ProjectM;
use app\common\model\UserProject as UserProjectM;
use app\common\model\ProjectCategory as ProjectCategoryM;

/** 核心或第三方类引入区 **/

use my\Wx;
use think\Db;
use think\Validate;

/**
 *  项目控制器
 * @package app\api\controller
 */
class Project extends Base
{
    public $noNeedLogin = ['*'];


    /**
     *  项目分类
    **/
    public function getCateList()
    {
        $store_id         = $this->store_id;
        $ProjectCategoryM = new ProjectCategoryM();
        $list             = $ProjectCategoryM->getProjectCate($store_id);
        $this->result('获取成功',$list,200);
    }


    /**
     *  项目列表
    **/
    public function getProjectList()
    {
        $store_id  = $this->store_id;
        $cate_id   = $this->request->post('cate_id',0);
        $limit     = $this->request->post('limit',10);

        $ProjectM   = new ProjectM();
        $list       = $ProjectM->getProjectList($store_id,$cate_id,0,0,0,'',$limit);

        $this->result('获取成功',$list,200);
    }


    /**
     *  查找 所属项目下的技师
    **/
    public function getEngineerList()
    {
        $store_id     = $this->store_id;
        $project_id   = $this->request->post('project_id',0);

        $UserProjectM   = new UserProjectM();
        $list           = $UserProjectM->getEngineer($project_id);
        $this->result('获取成功',$list,200);
    }


    /**
     * 获取项目分类下的技师
     */
    public function getCateEngineerList () {

        $project_category_id  = $this->request->post('project_category_id',0);
        $limit     = $this->request->post('limit',10);

        $where['p.project_category_id'] = $project_category_id;
        $where['p.store_id'] = $this->store_id;
        $list = Db::name('qt_project')->alias('p')
            ->leftJoin('qt_user_project up','p.id=up.project_id')
            ->leftJoin('qt_users u','u.id = up.engineer_id')
            ->field('u.id,u.real_name,u.avatar,u.sign')
            ->order('id desc')
            ->group('engineer_id')
            ->where($where)
            ->paginate($limit)
            ->each(function($item){
                $item['avatar'] = get_img_path($item['avatar']);

                return $item;
            });
        $this->result('获取成功',$list,200);

    }


    /**
     * 获取技师可做项目列表
     */
    public function getEngineerProject(){
        $limit     = $this->request->post('limit',10);
        $engineer_id = $this->request->post('engineer_id',0);
        $list = Db::name('qt_user_project')->alias('up')
            ->leftJoin('qt_project p','p.id=up.project_id')
            ->leftJoin('qt_users u','u.id = up.engineer_id')
            ->field('up.id,up.project_id,u.real_name,u.avatar,p.name as project_name,price,add_block_price,images,time,add_block_time')
            ->where('up.engineer_id',$engineer_id)
            ->paginate($limit)
            ->each(function($item){
                $item['avatar'] = get_img_path($item['avatar']);
                $images = get_album_array_path($item['images']);
                $item['project_img'] = !empty($images) ?  $images[0]: "";//取第一个主图
                return $item;
            });

        $this->result('获取成功',$list,200);
    }


}
